using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Impact.Controllers;
using Impact.Web.Foundation;
using Impact.Model.Objects;
using Impact.Web.Foundation.Controls;

namespace Impact.Web.Admin
{
    /// <summary>
    /// code-behind for the page listing page in the admin
    /// </summary>
    public partial class Pages : Impact.Web.Foundation.PageBase
    {
        private PageController controller;

        /// <summary>
        /// override of onload to perform startup functionality
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            controller = new PageController(UserId);
            controller.Initialize();

            base.OnLoad(e);

        }

        /// <summary>
        /// overridden to handling binding on this page
        /// </summary>
        protected override void Bind()
        {
            ImpactPageCollection pages = controller.GetPages();
            if (pages.Count > 0)
            {
                pageList.DataSource = pages;
                pageList.DataBind();
            }
            else
            {
                pageList.Visible = false;
                noPagesPanel.Visible = true;
            }
        }

        /// <summary>
        /// event handler for when a button is clicked in the page listing repeater
        /// </summary>
        protected void PageList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int pageId = Int32.Parse(e.CommandArgument.ToString());
            switch (e.CommandName)
            {
                case "delete":
                    controller.DeletePage(pageId);
                    break;

                case "up":
                    controller.DecrementSequence(pageId);
                    break;

                case "down":
                    controller.IncrementSequence(pageId);
                    break;

                case "toggle":
                    controller.ToggleEnabled(pageId);
                    //MessageBox.Show( string.Format( "Page #{0} toggled.", pageId ) );
                    break;

                case "preview":
                    Redirect(string.Format("!preview.ashx?pageId={0}", pageId));
                    break;

                default:
                    throw new ArgumentException(string.Format("Command not found: {0}", e.CommandName));
            }
            Bind();
        }
    }
}
